"""
从格五种：
    从财格
    从杀格
    从儿格
    从势格
    从气格
"""
from ..consts.zhi_consts import ZHIS_3HE, ZHIS_3HUI
HE_MP = {item[3]: item[:3] for item in ZHIS_3HE}
HUI_MP = {item[3]: item[:3] for item in ZHIS_3HUI}


def cong_cai(mp):
    """
    从财格
    """
    zhis = [zhu[1].zhi.name for zhu in mp.sizhu]
    cai_wx = mp.biroles['财星']
    # cond1
    cond = mp.is_very_weak()
    # cond2
    cond = cond and mp.yueling.zhi.wuxing.name == cai_wx
    # cond3
    cond3_1 = all([zhu[1].zhi.wuxing.name == cai_wx for zhu in mp.sizhu])
    cond3_2 = all([item in zhis for item in HE_MP[cai_wx]]) if cai_wx != '土' else False
    cond3_3 = all([item in zhis for item in HUI_MP[cai_wx]]) if cai_wx != '土' else False
    cond = cond and (cond3_1 or cond3_2 or cond3_3)
    return cond


def cong_sha(mp):
    """
    从杀格
    """
    sha_wx = mp.biroles['官杀']
    # cond1
    cond = mp.is_very_weak()
    # cond2
    cond = cond and mp.yueling.zhi.wuxing.name == sha_wx
    sha_ct = sum([sum([1 if cell.wuxing() == sha_wx else 0 for cell in zhu]) for zhu in mp.sizhu])
    # cond2
    cond = cond and (sha_ct >= 4)
    return cond


def cong_er(mp):
    """
    从儿格
    """
    er_wx = mp.biroles['食伤']
    zhis = [zhu[1].zhi.name for zhu in mp.sizhu]
    # cond1
    cond = mp.is_very_weak()
    cond2_1 = all([zhu[1].zhi.wuxing.name == er_wx for zhu in mp.sizhu])
    cond2_2 = all([item in zhis for item in HE_MP[er_wx]]) if er_wx != '土' else False
    cond2_3 = all([item in zhis for item in HUI_MP[er_wx]]) if er_wx != '土' else False
    return cond and (cond2_1 or cond2_2 or cond2_3)


def cong_wang(mp):
    """
    从旺格
    """
    sha_wx = mp.biroles['官杀']
    my_wx = mp.riyuan.wuxing()
    for tg_cell, dz_cell in mp.sizhu:
        if tg_cell.wuxing() == sha_wx or dz_cell.wuxing() == sha_wx:
            return False
        elif tg_cell.wuxing() != my_wx and dz_cell.wuxing() != my_wx:
            return False
    return True


def cong_qiang(mp):
    """
    从强格
    """

    cai_wx = mp.biroles['财星']
    sha_wx = mp.biroles['官杀']
    yin_wx = mp.biroles['印绶']
    my_wx = mp.riyuan.gan.wuxing
    if not my_wx.same_as(mp.yueling.zhi.wuxing) and not my_wx.bei_sheng(mp.yueling.zhi.wuxing):
        return False
    for tg_cell, dz_cell in mp.sizhu:
        if tg_cell.wuxing() in [cai_wx, sha_wx] or dz_cell.wuxing() in [cai_wx, sha_wx]:
            return False
        elif tg_cell.wuxing() != yin_wx and dz_cell.wuxing() != yin_wx:
            return False
    return True
